package com.zx.mes.rocketmq.hyl.quickstart;

import com.zx.mes.rocketmq.hyl.constant.Const;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * 延迟队列
 * rocket mq 为了性能没有开放自定义延迟时间接口(源码类固定设置延迟级别在哪??)
 *
 * @author 华云龙
 * @date 2018-11-30
 */
public class DelayProducer {

    private static final int INT = 5;
    private static final String TEST_QUICK_TOPIC = "test_quick_topic";

    public static void send() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        String producerGroup = "test_quick_producer_name";
        DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
        producer.setNamesrvAddr(Const.NAMESRV_ADDR_SINGLE);

        producer.start();
        for (int i = 0; i <INT ; i++) {
            Message message = new Message(TEST_QUICK_TOPIC,"Delay tags","delay:"+i,("延迟投递:"+i).getBytes());
            if (i == 1) {
                message.setDelayTimeLevel(3);
            }
            SendResult result = producer.send(message);
            System.out.println(result);
        }

        producer.shutdown();
    }
}
